package com.example.bookstore.service.impl;

import com.example.bookstore.entity.BookUser;
import com.example.bookstore.entity.Product;
import com.example.bookstore.enums.ResultEnum;
import com.example.bookstore.enums.TokenEnum;
import com.example.bookstore.exception.ResultException;
import com.example.bookstore.form.bookform.InsertBookForm;
import com.example.bookstore.form.bookform.UpdateBookForm;
import com.example.bookstore.form.userform.LoginForm;
import com.example.bookstore.mapper.ProductMapper;
import com.example.bookstore.security.relam.UserRealm;
import com.example.bookstore.service.BookService;
import com.example.bookstore.token.LoginToken;
import com.example.bookstore.utils.FileUpload;
import com.example.bookstore.utils.PageUtil;
import com.example.bookstore.utils.ResultVOUtil;
import com.example.bookstore.vo.LoginVo;
import com.example.bookstore.vo.ResultVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Repository
public class BookServiceImpl implements BookService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public ResultVO InsertBook(InsertBookForm insertBookForm) {

        Product product =  new Product();
        product.setproductName(insertBookForm.getProductName());
        product.setproductPrice(insertBookForm.getProductPrice());
        product.setproductPnum(insertBookForm.getProductPnum());
        product.setproductClass(insertBookForm.getProductClass());
        product.setDescription(insertBookForm.getDescription());

        //product.setimgUrl(FileUpload.UploadPicture(file));

        try {
            productMapper.InsertBook(product);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(product);
    }

    @Override
    public ResultVO UpdateBook(UpdateBookForm updateBookForm) {

        try{
            productMapper.UpdateBook(updateBookForm);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO UpdateBookImg(MultipartFile file, Integer productId) {
        String imgUrl = FileUpload.UploadPicture(file);
        Product product = new Product();
        try {
            productMapper.UpdateBookImg(imgUrl,productId);
            product = productMapper.SelectBookById(productId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(product.getimgUrl());
    }

    @Override
    public ResultVO DeleteBook(Integer productId) {
        try{
            productMapper.DeleteBook(productId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO SelectBookById(Integer productId) {
        Product product = new Product();
        try {
            product = productMapper.SelectBookById(productId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(product);
    }

    @Override
    public ResultVO SelectBookByClass(String productClass) {
        List<Product> productList = new ArrayList<>();
        try{
            productList = productMapper.SelectBookByClass(productClass);
        }catch (Exception e){
            e.printStackTrace();
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(productList);

        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO SelectBookByName(String productName) {
        HashMap<String,Object> hashMap = new HashMap<>();
        List<Product> productList = new ArrayList<>();
        try {
            productList = productMapper.SelectBookByName(productName);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        hashMap = PageUtil.getMap(productList);
        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO ShowRandomBook() {
        List<Product> productList = new ArrayList<>();
        try {
            productList = productMapper.ShowRandomBook();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(productList);
    }

    @Override
    public ResultVO ShowAllBook() {
        List<Product> productList = new ArrayList<>();
        try{
            productList = productMapper.ShowAllBook();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(productList);

        return ResultVOUtil.success(hashMap);
    }


}
